package com.aier.classes.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.classes.entity.ClassParent;
import com.aier.common.common.SystemDateFormatUtil;


public class ClassParentRowMapper implements RowMapper<ClassParent> {

    private int type;

    public static final int AND_STUDENTNAME = 1;
    
    public static final int AND_NAMES = 2;
    
    public static final int AND_ACCOUNT = 3;
    
    public static final int AND_ACC = 4;
    
    public ClassParentRowMapper() {
    }

    public ClassParentRowMapper(int type) {
        this.type = type;
    }

    @Override
    public ClassParent mapRow(ResultSet rs, int rowNum) throws SQLException {
        ClassParent cp = parseClassParent(new ClassParent(), rs);
        cp.setCtime_str(SystemDateFormatUtil.formatDate(cp.getCtime(), SystemDateFormatUtil.FORMAT_DATETIME_NOTSECOND));

        switch (type) {
            case AND_STUDENTNAME:
                cp.setName_student(rs.getString("name_student"));
                cp.setTel(rs.getString("tel"));
                cp.setEdate(rs.getString("edate"));
                break;
            case AND_NAMES:
                cp.setName_student(rs.getString("name_student"));
                cp.setName_classes(rs.getString("name_classes"));
                cp.setClogo(rs.getString("clogo"));
                break;
            case AND_ACCOUNT:
            	cp.setName_student(rs.getString("name_student"));
            	cp.setAccount(rs.getString("account"));
            	break;
            case AND_ACC:
                cp.setAccount(rs.getString("account"));
                break;
        }
        return cp;
    }

    public static ClassParent parseClassParent(ClassParent s, ResultSet rs) throws SQLException {
        return parseClassParent(s, rs, null);
    }

    public static ClassParent parseClassParent(ClassParent s, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            s.setCpid(rs.getLong(su+"cpid"));
        } catch (SQLException e) {
            s.setCpid(null);
        }
        try {
            s.setUid(rs.getLong(su+"uid"));
        } catch (SQLException e) {
            s.setUid(null);
        }
        try {
            s.setCid(rs.getLong(su+"cid"));
        } catch (SQLException e) {
            s.setCid(null);
        }
        try {
            s.setPname(rs.getString(su+"pname"));
        } catch (SQLException e) {
            s.setPname(null);
        }
        try {
            s.setCsid(rs.getLong(su+"csid"));
        } catch (SQLException e) {
            s.setCsid(null);
        }
        try {
            s.setCtime(rs.getTimestamp(su+"ctime"));
        } catch (SQLException e) {
            s.setCtime(null);
        }
        try {
            s.setRelation(rs.getString(su+"relation"));
        } catch (SQLException e) {
            s.setRelation(null);
        }
       
        return s;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, ClassParent s) {
        par.put("cpid",s.getCpid());
        par.put("uid",s.getUid());
        par.put("cid",s.getCid());
        par.put("pname", s.getPname());
        par.put("csid",s.getCsid());
        par.put("ctime",s.getCtime());
        par.put("relation",s.getRelation());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, ClassParent s) {
        args.put("cpid",s.getCpid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final ClassParent s) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(s),
                        new String[] { "cpid" });
                parsePS(ps, s);
                return ps;
            }

        };
    }

    private static String parseSQL(ClassParent s) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into class_parent(");
        int count = 0;
        if (null != s.getUid()) {
            sql.append("uid,");
            count++;
        }

        if (null !=s.getCid()) {
            sql.append("cid,");
            count++;
        }
        if (null !=s.getPname()) {
            sql.append("pname,");
            count++;
        }
        if (null != s.getCsid()) {
            sql.append("csid,");
            count++;
        }
        if (null != s.getCtime()) {
            sql.append("ctime,");
            count++;
        }
        if (null != s.getRelation()) {
            sql.append("relation,");
            count++;
        }

        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, ClassParent s)
        throws SQLException {
        int count = 1;
        if (null != s.getUid()) {
            ps.setLong(count++,s.getUid());
        }
        if (null != s.getCid()) {
            ps.setLong(count++, s.getCid());
        }
        if (null != s.getPname()) {
            ps.setString(count++, s.getPname());
        }
        if (null != s.getCsid()) {
            ps.setLong(count++,s.getCsid());
        }
        if (null != s.getCtime()) {
            ps.setTimestamp(count++,s.getCtime());
        }
        if (null != s.getRelation()) {
            ps.setString(count++,s.getRelation());
        }
       


    }

}
